#include "mainwindow.h"
#include "rpi_sensors.h"
#include "rpi_control.h"
#include "spinBoxDelegate.h"
#include "comboBoxDelegate.h"
#include "ControlModel.h"
#include "ControlModelItem.h"
#include <QStandardItemModel>

controlsWidget::controlsWidget(QObject *parent,QList<rpi_control*> *l) {
    setupUi(this);
    controls =l;
    refreshContent();
    //cm = new ControlModel(l,this);
    //tableWidget->setModel(cm);
    tableWidget->setColumnCount(5);
    tableWidget->setRowCount(0);

    QStringList headers;
    headers << "Type" << "Device"
                << "Address" << "Node" << "value";
    tableWidget->setHorizontalHeaderLabels(headers);
    //connect(tableWidget,SLOT())
    spinDelegate = new EightBitSpinBox;
    comboDevices = new DeviceDelegate;
    comboTypes = new ComboBoxDelegate;
    comboTypes->listItems << "Servo" << "BiDectional" << "ESC Motor";
    comboDevices->listItems << "/dev/spidev0.0" << "/dev/spidev1.0" ;

    tableWidget->setItemDelegateForColumn(0,comboTypes);
    tableWidget->setItemDelegateForColumn(1,comboDevices);

    tableWidget->setItemDelegateForColumn(2,spinDelegate);
    tableWidget->setItemDelegateForColumn(3,spinDelegate);
    tableWidget->setItemDelegateForColumn(4,spinDelegate);

    connect(tableWidget,SIGNAL(cellChanged(int,int)),
            this,SLOT(ControlChanged(int,int)));
            /*tableView->setItemDelegateForColumn(1,comboTypes);
    tableView->setItemDelegateForColumn(2,comboDevices);

    tableView->setItemDelegateForColumn(3,spinDelegate);
    tableView->setItemDelegateForColumn(4,spinDelegate);
    tableView->setItemDelegateForColumn(5,spinDelegate);
    */

    //QList<QVariant> *data =new QList<QVariant>;
    //rModelItem = new ControlItem(data);
    cModel = 0;
    sModel =0;
    ignoreChanges =false;
    //connect(powerSlider,SIGNAL(valueChanged(int)),this,SLOT(setPower(int)));
    //connect(steerSlider,SIGNAL(valueChanged(int)),this,SLOT(setSteer(int)));
    //connect(stopBtn,SIGNAL(clicked()),this,SLOT(stopClicked()));
    
    

}

void controlsWidget::stopClicked() {
//steerSlider->setValue(128);
//powerSlider->setValue(128);
}


void controlsWidget::setSteer(int v) {
if(controls->size() <= 1) return;
//(*controls)[1]->setValue(steerSlider->value());
emit controlUpdate(1);
}

void controlsWidget::setPower(int v) {
if(controls->size() <= 0) return;
(*controls)[0]->setValue(powerSlider->value());
emit controlUpdate(0);
//emit sendFastControl(1,powerSlider->value());
    //for (int k=0;k<controls->size();k++) {
        //if (((*controls)[k]->type() == rpi_control::RPI_BIMOTOR) || ((*controls)[k]->type() == rpi_control::RPI_ESCMOTOR)) {
            //(*controls)[k]->setValue(v);
            //controlUpdate(k);
            //refreshControl(k);
            
        //}
    //}
    /*for (int k=0;tableWidget->rowCount();k++) {
        int type = tableWidget->item(k,0)->data(Qt::DisplayRole).toInt();
        if (type == 2) {
            QTableWidgetItem *value = new QTableWidgetItem(QString("%1").arg(v));
            tableWidget->setItem(k,4,value);
        }
    }*/


}

void controlsWidget::ControlChanged(int row, int column){
    if (ignoreChanges) return;
    qDebug() << "itemWasEdited" << row << column;

    QString itemText = tableWidget->item(row,column)->text();

    switch (column) {
        case COL_TYPE : {
            int setType = comboTypes->listItems.indexOf(itemText);
            if (setType != -1) (*controls)[row]->setType(setType);
            break;}
        case COL_ADDRESS : {
            (*controls)[row]->setAddress(itemText.toUInt());
            break;}
        case COL_DEVICE : {
            (*controls)[row]->setDevice(itemText);
            break;}
        case COL_NODE: {
            (*controls)[row]->setNode(itemText.toUInt());
            break;}
        case COL_VALUE : {
            (*controls)[row]->setValue(itemText.toUInt());
            break;}
        default : {break;}
    }
    emit controlUpdate(row);

}

controlsWidget::~controlsWidget() {
    delete spinDelegate;
    delete comboTypes;
    delete comboDevices;
    if (cModel) delete cModel;
    if (sModel) delete sModel;

}


void controlsWidget::refreshContent() {
    qDebug() << "Content Controls Refresh" << controls->size();
    if (!controls) return;


    tableWidget->setRowCount(controls->size());

    //ControlModel *newControlModel = new ControlModel(controls);
    //tableView->setModel(newControlModel);
    //if (cModel) delete cModel;
    //cModel = newControlModel;
    //return;


    //QStandardItemModel* tModel = new QStandardItemModel(
    //            controls->size(),6);
    for (int k=0;k<controls->size();k++) {
        refreshControl(k);
    }}
void controlsWidget::refreshControl(int k){
    ignoreChanges = true;
    int type_t =(*controls)[k]->type();

    QString itemTypeText ;
    if (type_t < comboTypes->listItems.size()) {
       itemTypeText = comboTypes->listItems[(*controls)[k]->type()];
    }else {
        itemTypeText = comboTypes->listItems[0];
    }
    QTableWidgetItem *itemId = new QTableWidgetItem(QString("%1").arg((*controls)[k]->id()));
    QTableWidgetItem *itemType = new QTableWidgetItem(itemTypeText);
    QTableWidgetItem *itemDevice = new QTableWidgetItem((*controls)[k]->device());
    QTableWidgetItem *itemAddress = new QTableWidgetItem(QString("%1").arg((*controls)[k]->address()));
    QTableWidgetItem *itemNode = new QTableWidgetItem(QString("%1").arg((*controls)[k]->node()));
    QTableWidgetItem *itemValue = new QTableWidgetItem(QString("%1").arg((*controls)[k]->value()));

    //tableWidget->setItem(k+0,0,itemId);
    tableWidget->setItem(k+0,0,itemType);
    tableWidget->setItem(k+0,1,itemDevice);
    tableWidget->setItem(k+0,2,itemAddress);
    tableWidget->setItem(k+0,3,itemNode);
    tableWidget->setItem(k+0,4,itemValue);
    ignoreChanges = false;

    }//*/


